import React,{Component} from 'react'
import {Link,browserHistory} from 'react-router'
import { message } from 'antd';
import * as fetch from '../config/fetch';
import * as pub from '../config/public';
class BackPassword extends Component {
	constructor(args) {
		super();
		this.state = {
			time:60
		}
	}
	submit(e) {
		e.preventDefault();
		if (this.refs.newPassWord.value !== this.refs.cfmPassWord.value) {
			message.error('密码不相同', 3);
			return;
		}
		fetch.changePassword(this.refs.phone.value, this.refs.phoneCode.value, this.refs.newPassWord.value).then((data) => {
			if (data.result === 'RC100') {
				this.login();
			} else {
				message.error(data.errMsg, 3);
			}

		})
	}
	login() {

		let user = {
			name: this.refs.phone.value,
			password: this.refs.newPassWord.value
		};

		fetch.login(user.name, user.password).then((data) => {
			if (data.result === 'RC100') {

				let info = data.menu;

				let menu = {};
				menu.admin_menu = [];
				menu.learn_menu = [];
				menu.personal_menu = [];
				let menu_data = [];
				let job = [];

				for (let user of info) {
					job.push(user.job);
					if (user.job === '学员') {
						menu_data = user.nodeResult;
					}
					if(user.job === '管理员'){
						menu.admin_menu = user.nodeResult;
					}
				}

				for(let x of menu_data){
					if(x.parentNodeCode === '0'){
						menu.learn_menu.push(x);
					}else{
						menu.personal_menu.push(x);
					}
				}
				let user = {};
				user.ldToken = data.ldToken;
				user.userType = data.userType;
				user.userCode = data.userCode;
				user.job = job;
				user.menu = menu;
				user.username = this.state.username;

				
				localStorage.setItem("isRemember", this.state.isRemember);
				if(this.state.isRemember){
					localStorage.setItem("username", this.state.username);
					localStorage.setItem("password", this.state.password);
				}

				localStorage.setItem("user", JSON.stringify(user));
				pub.save_user();
				pub.jumpHistory.push('/Entrance');

			} else {
				message.error(data.errMsg, 3);
			}
		}, (res) => {
			pub.reject(res);
		})

	}
	getCode(e) {
		if(!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(this.refs.phone.value))){
			message.error('请输入正确的手机号',1);
			return;
		}
		fetch.getPhoneCode(this.refs.phone.value).then((data) => {
			if(data.result === 'RC100'){
				message.success(data.errMsg,1);
			}else{
				message.error(data.errMsg,1);
				return;
			}
			this.get_code = setInterval(() => {
				if (this.state.time === 0) {
					this.clearTimer();
					this.setState({
						time: 60
					})
					return;
				}
				this.setState({
					time: this.state.time - 1
				}, () => {})
			}, 1000);
		})
	}
	clearTimer() {
		this.get_code && clearTimeout(this.get_code);
		clearInterval(this.get_code);
	}
	InputNumber(e){
		  let value= this.refs.phone.value
		 if(value.length>11){
			e.target.value="";
			message.error("只能为11位数");
		 }else{
				e.target.value=value.replace(/[^\d]/g,'')
		 }
	}
	InputNumbers(e){
		let value=this.refs.newPassWord.value
		if(value!==""){
		e.target.value=value.replace(/^ +| +$/g,'')
		}
	
	}
	InputNumbeo(e){
		let value=this.refs.cfmPassWord.value
		if(value!==""){
			e.target.value=value.replace(/^ +| +$/g,'')
			}
	}
	render(){
		return(
		<div className="login-bg login-bg-1">
			<div className="login-box">
				<div className="login-logo"><Link to='Login'><div className="logo"></div></Link><img className="login-logoImg" alt='test' src={require('../style/images/font-style@2x.png')} width="200" style={{verticalAlign: 'middle'}}/></div>
				    <form onSubmit={(e)=>this.submit(e)} className="psw-input" autoComplete="off">
				    	<label>
				    	       <input type="text" ref='phone' required="required" placeholder="手机号" onChange={(e)=>this.InputNumber(e)} />
				    	       {this.state.time === 60 ?
				    	       <label className="btnGetCode" onClick={(e)=>this.getCode(e)}>获取验证码</label>
				    	       :
				    	       <label className="btnGetCode" style={{color:'#aaa',cursor:'default'}}>{`重新获取(${this.state.time}S)`}</label>
				    	       }
				    	</label>
				    	<label><input type="text" ref='phoneCode' required="required" placeholder="验证码" /></label>
				    	<label><input type="password" ref='newPassWord' required="required" placeholder="新密码" onChange={(e)=>this.InputNumbers(e)} /></label>
					    <label><input type="password" ref='cfmPassWord' required="required" placeholder="确认密码" onChange={(e)=>this.InputNumbeo(e)}/></label>
					   
				    	<button type='submit' className="login-btn next-btn">确认并登录</button>
				    </form>
		


			</div>
		</div>
			)
	}
}
export default BackPassword;